package com.ychs.servlet;

import com.ychs.entity.College;
import com.ychs.entity.Major;
import com.ychs.entity.OpenClass;
import com.ychs.service.IBaseService;
import com.ychs.service.OpenClassService;
import com.ychs.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/24
 */
@WebServlet(value = "/OpenClassServlet")
public class OpenClassServlet extends BaseServlet {
    private IBaseService<OpenClass> service = new OpenClassService();
    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter openClass add method");
        String classDate = request.getParameter("classDate");
        String subject = request.getParameter("subject");
        String speaker = request.getParameter("speaker");
        String organize = request.getParameter("organize");
        String collegeId = request.getParameter("college");
        String majorId = request.getParameter("major");
        String place = request.getParameter("place");
        String groupShow = request.getParameter("groupShow");
        String way = request.getParameter("way");
        String score = request.getParameter("score");
        String participants = request.getParameter("participants");
        String remark = request.getParameter("remark");

        College college = new College();
        Major major = new Major();
        college.setId(Integer.parseInt(collegeId));
        major.setId(Integer.parseInt(majorId));

        OpenClass openClass = new OpenClass();
        openClass.setClassDate(DateUtil.StringToDate(classDate));
        openClass.setSubject(subject);
        openClass.setSpeaker(speaker);
        openClass.setOrganize(organize);
        openClass.setCollege(college);
        openClass.setMajor(major);
        openClass.setPlace(place);
        openClass.setGroupShow(groupShow);
        openClass.setWay(way);
        openClass.setScore(Integer.parseInt(score));
        openClass.setParticipants(Integer.parseInt(participants));
        openClass.setRemark(remark);

        boolean result = service.add(openClass);
        logger.info("add result:  " + result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter openClass modify method");
        String id = request.getParameter("id");
        String classDate = request.getParameter("classDate");
        String subject = request.getParameter("subject");
        String speaker = request.getParameter("speaker");
        String organize = request.getParameter("organize");
        String collegeId = request.getParameter("college");
        String majorId = request.getParameter("major");
        String place = request.getParameter("place");
        String groupShow = request.getParameter("groupShow");
        String way = request.getParameter("way");
        String score = request.getParameter("score");
        String participants = request.getParameter("participants");
        String remark = request.getParameter("remark");

        College college = new College();
        Major major = new Major();
        college.setId(Integer.parseInt(collegeId));
        major.setId(Integer.parseInt(majorId));

        OpenClass openClass = new OpenClass();
        openClass.setId(Integer.parseInt(id));
        openClass.setClassDate(DateUtil.StringToDate(classDate));
        openClass.setSubject(subject);
        openClass.setSpeaker(speaker);
        openClass.setOrganize(organize);
        openClass.setCollege(college);
        openClass.setMajor(major);
        openClass.setPlace(place);
        openClass.setGroupShow(groupShow);
        openClass.setWay(way);
        openClass.setScore(Integer.parseInt(score));
        openClass.setParticipants(Integer.parseInt(participants));
        openClass.setRemark(remark);

        boolean result = service.modify(openClass);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter openClass remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter openClass search method");
        Map<String, Object> conditions = new HashMap<>();

        String dateRange = request.getParameter("searchDateRange");
        logger.info(dateRange);
        String college = request.getParameter("searchCollege");
        String major = request.getParameter("searchMajor");

        if(dateRange!=null&&!"".equals(dateRange)){
            String beginDate = dateRange.substring(0, 7);
            logger.info(DateUtil.StringToDate(beginDate));
            String endDate = dateRange.substring(10, 17);
            logger.info(DateUtil.StringToDate(endDate));
            conditions.put("beginDate", DateUtil.StringToDate(beginDate));
            conditions.put("endDate", DateUtil.StringToDate(endDate));
        }
        if(college!=null&&!"".equals(college)){
            conditions.put("college",college);
        }
        if(major!=null&&!"".equals(major)){
            conditions.put("major",major);
        }

        int count = service.searchTotalNumber(conditions);

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        if (page != null && limit != null) {
            int begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
            int pageSize = Integer.parseInt(limit);
            conditions.put("begin", begin);
            conditions.put("pageSize", pageSize);
        }

        List<OpenClass> openClasses = service.search(conditions);

        String json = json(count, openClasses);
        logger.info(json);
        response.getWriter().write(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter openClass searchById method");
        String id = request.getParameter("id");
        OpenClass openClass = service.searchById(Integer.parseInt(id));
        String json = json(openClass);
        logger.info("selectById"+json);
        response.getWriter().write(json);
    }
}
